package com.yohann.algorithm;

/**
 * <p>
 * 队列
 * </p>
 *
 * @author Yohann
 * @since 2020/9/21
 */
public class QueueTest {
    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(3);
        arrayQueue.addQueue(1);
        arrayQueue.addQueue(2);
        arrayQueue.addQueue(3);
        arrayQueue.show();
        System.out.println(arrayQueue.getQueue());
        System.out.println(arrayQueue.getQueue());
        System.out.println(arrayQueue.getQueue());
    }
}

class ArrayQueue{
    //最大容量
    private int maxSize;
    //队列头部
    private int front;
    //队列尾部
    private int rear;
    //存储队列的数组
    private int[] arr;

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.arr = new int[maxSize];
        //指向队列头前一个位置
        this.front = -1;
        //指向队列尾部（包含队列最后一个数据）
        this.rear = -1;
    }

    public boolean isFull(){
        return rear == maxSize-1;
    }

    public boolean isEmpty(){
        return rear == front;
    }

    public void addQueue(int num){
        //判断队列是否满
        if (isFull()){
            System.out.println("已满！");
            return;
        }

        arr[++rear] = num;
    }

    public int getQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空！");
        }

        return arr[++front];
    }

    public void show(){
        if (isEmpty()){
            System.out.println("队列为空！");
            return;
        }

        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]+ "  ");
        }
    }

    public int headQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空！");
        }

        return arr[++front];
    }
}